var isDisplayed = false;
var tabDisplay = new Array();
var tabIsActive = new Array();
$(document).ready( function(){ 
	$("#selectRes").change(function(optionSelected){
		if( optionSelected.currentTarget.value > -1){
			$("#loading").css('display','block');
			recupJours($("#selectionDate")[0].value);
			displayICalLink();
			recupEdtSemaine();
		}
	});
	
	$("#selectionDate").change(function(optionSelected){
		if( $("#selectRes")[0].value > -1){
			$("#loading").css('display','block');
			recupJours(optionSelected.currentTarget.value);
			recupEdtSemaine();
		}
	});
	
	$("#selectComp").change(function(optionSelected){
		$("#selectRes").attr('disabled', true);
		if( optionSelected.currentTarget.value > -1){
			$.ajax({
				url:"recupSections.php?id="+optionSelected.currentTarget.value,
				success:function(data){
					$("#selectRes").html(data);
					$("#selectRes").attr('disabled', false);
					//$("#loading").css('display','none');
					}
			});
		}
	});
	
	$("#ical").click(function(source){
		$("#icallink").select();
	});
	
	$("body").mousemove(function(optionSelected){
		if (optionSelected.target.className == "infosImage")
		{
			var x = optionSelected.clientX-optionSelected.target.offsetLeft - 55;
			var y = optionSelected.clientY-optionSelected.target.offsetTop - 72;
			
			if (isDisplayed == true && (x>20 || y>60) )//optionSelected.target != currentDiv)
			{
				tabIsActive[optionSelected.target.parentNode.lastChild.lastChild.value] = false;
				isDisplayed = false;
				$("#infosCours").css('display','none');
				$("#infosCours").html("");
			}
			if (isDisplayed == false && x<20 && y<60)
			{
				tabIsActive[optionSelected.target.parentNode.lastChild.lastChild.value] = true;
				afficheCadre(optionSelected.target.parentNode.lastChild.lastChild.value, optionSelected.clientX, optionSelected.clientY);
			}
		}
		else if (optionSelected.target.className.indexOf('infosCours') == -1 && isDisplayed == true)
		{
			for (var i = 0; i< tabIsActive.length; i++)
			{
				if (i in tabIsActive)
					tabIsActive[i] = false;
			}
			isDisplayed = false;
			$("#infosCours").css('display','none');
			$("#infosCours").html("");
		}
		
	});

	$("#btnSemainePrecedente").click(function(eventObject){
		if ($("#selectionDate")[0].selectedIndex > 0)
		{
			$("#selectionDate")[0].selectedIndex --;
			if ($("#selectRes")[0].value > -1)
			{
				recupJours($("#selectionDate")[0].value);
				$("#loading").css('display','block');
				recupEdtSemaine();
			}
		}
	});
	
	$("#btnSemaineSuivante").click(function(eventObject){
		if ($("#selectionDate")[0].selectedIndex < 12)
		{
			$("#selectionDate")[0].selectedIndex ++;
			if ($("#selectRes")[0].value > -1)
			{
				recupJours($("#selectionDate")[0].value);
				$("#loading").css('display','block');
				recupEdtSemaine();
			}
		}
	});
});

jQuery.fn.selText = function() {
    var obj = this[0];
    if ($.browser.msie) {
        var range = obj.offsetParent.createTextRange();
        range.moveToElementText(obj);
        range.select();
    } else if ($.browser.mozilla || $.browser.opera) {
        var selection = obj.ownerDocument.defaultView.getSelection();
        var range = obj.ownerDocument.createRange();
        range.selectNodeContents(obj);
        selection.removeAllRanges();
        selection.addRange(range);
    } else if ($.browser.safari) {
        var selection = obj.ownerDocument.defaultView.getSelection();
        selection.setBaseAndExtent(obj, 0, obj, 1);
    }
    return this;
}

function displayICalLink(){
	$("#ical").css('display', 'block');
	$("#icallink")[0].value = "http://bastienrene.fr/EDT/recupEDTICal.php?idRes="+$("#selectRes")[0].value; 
}

function afficheCadre(id, x, y)
{
	//alert("test");
	//currentDiv = optionSelected.currentTarget;
	isDisplayed = true;
	$("#infosCours").css('left', x);
	$("#infosCours").css('top',y);
	$("#infosCours").css('display','block');
	$("#infosCours").html("<div class='loading infosCours'><img class='infosCours' src='images/loading_petit.gif'/></div>");
	if (tabDisplay[id] != undefined && tabDisplay[id] != "recup")
	{
		$("#infosCours").html(tabDisplay[id])
	}
	else if (tabDisplay[id] != "recup")
	{
		tabDisplay[id] = "recup";
		$.ajax({
			url:"recupSession.php?session="+$('#session')[0].value+ "&time="+$('#time')[0].value,
			success:function(data){
				$("#divSession").html(data);
				if ($("#session")[0].value != -1)
				{
					$.ajax({
						url:"recupActivities.php?idActivity="+id+"&actualWeek="+$("#selectionDate")[0].value+"&session="+$("#session")[0].value, //http://localhost/EDT/recupActivities.php?idActivity=14906
						success:function(data){
							if (data.indexOf("Operation") == -1)
							{
								if (tabIsActive[id] == true)
								{
									$("#infosCours").html(data);
									$("#loading").css('display','none');
								}
								tabDisplay[id] = data;
							}
							else
							{
								tabDisplay[id] = undefined;
								$("#loading").css('display','none');
								$("#infosCours").html("<p class='infosCours'>Probl&egrave;me lors de la r&eacute;cup&eacute;ration</p>");
							}
						}
					});
				}
				else
				{
					$("#loading").css('display','none');
					$("#infosCours").html("<p>Probl&egrave;me lors de la r&eacute;cup&eacute;ration</p>");
				}
			}
		});
	}
}

function selectText(element) {
    var doc = document
        , text = doc.getElementById(element)
        , range, selection
    ;    
    if (doc.body.createTextRange) { //ms
        range = doc.body.createTextRange();
        range.moveToElementText(text);
        range.select();
    } else if (window.getSelection) { //all others
        selection = window.getSelection();        
        range = doc.createRange();
        range.selectNodeContents(text);
        selection.removeAllRanges();
        selection.addRange(range);
    }
}


function updateEDT(){
	$.ajax({
				url:"updateEDT.php",
				success:function(data){
					
					}
			});
}
			
function recupJours(week)
{
	$.ajax({
		url:"recupJours.php?week="+week,
		success:function(data){
			$("#joursEDT").html(data);
			displayTime();
			}
	});
}

function recupEdtSemaine(){
	$.ajax({
		url:"recupSession.php?session="+$('#session')[0].value+ "&time="+$('#time')[0].value,
		success:function(data){
			$("#divSession").html(data);
			//if ($("#session")[0].value != -1)
			//{
			if ($("#session")[0].value == -1)
				alert("Attention, cet emploi du temps est récupéré de la base de données, il peut ne pas être à jour");
			for (var i = 0; i< 5; i++)
			{
				recupEdtJour(i);
			}
			displayTime();
			//}
			//else
			//{
			//$("#loading").css('display','none');
			
			//}
		}
	});
}

function recupEdtJour(jour)
{
	$("#jour"+jour).html("");
	$.ajax({
		url:"recupEDT.php?idRes="+$("#selectRes")[0].value + "&week="+$('#selectionDate')[0].value+"&session="+$("#session")[0].value+"&jour="+jour,
		success:function(data){
			$("#jour"+jour).html(data);
			$("#loading").css('display','none');
			}
	});
}
	
var timer;
function displayTime(){
	var now = new Date();
	var heures = now.getHours(); 
	var minutes = now.getMinutes(); 
	//heure été
	var annee = now.getFullYear();
	if (heures >= 19){ heures = 19; minutes = 0; }
	if (heures < 8) { heures = 8; minutes = 0;}
	for (i=31; i >24; i--) {
		dimanche = new Date(annee, 2, i);
		if (dimanche.getDay()==0) break;
	}
	for (i=31; i >24; i--) {
		dimanche2 = new Date(annee, 9, i);
		if (dimanche2.getDay()==0) break;
	}
	if (now >= dimanche && now < dimanche2) heures ++;
	var test = false;
	
	var divJours = $(".jour");
	for (var i = 0; i < 5; i++){
		 var jour_contenu = divJours[i].innerText;
		 if (jour_contenu == undefined) jour_contenu = divJours[i].textContent;
		 //alert(jour_contenu);
		 var jour = jour_contenu.substr(jour_contenu.indexOf("/",0) - 2, 2);
		 jour_contenu = jour_contenu.substr(jour_contenu.indexOf("/",0)+1);
		 mois = jour_contenu.substr(0, 2);
		 if (jour == now.getDate() && mois == now.getMonth()+1){
			$("#trait_heure").css('left', i * 214 + 56);
			$("#trait_heure").css('top',(heures-8)*60+minutes+40);
			$("#trait_heure").css('display','block');
			test = true;
		}	
	}
	if (test == false)
		$("#trait_heure").css('display','none');
	clearTimeout(timer);
	timer = setTimeout("displayTime()",60000); 
}
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			